package com.sheng.whyclass.person.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.TeacherLoginEvent;

import de.greenrobot.event.EventBus;

public class TeacherLoginActivity extends Activity implements View.OnClickListener{

    private TextView title;
    private EditText teacherName;
    private EditText teacherPass;
    private Button teacherLoginBtn;
    private TextView studentLoginText;
    private TextView teacherRegisterText;


    private String teacher_name;
    private String teacher_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teacher_login);
        initView();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        teacherName = (EditText) findViewById(R.id.teacher_name);
        teacherPass = (EditText) findViewById(R.id.teacher_pass);
        teacherLoginBtn = (Button) findViewById(R.id.teacher_login_btn);
        teacherLoginBtn.setOnClickListener(this);
        studentLoginText = (TextView) findViewById(R.id.student_login_text);
        studentLoginText.setOnClickListener(this);
        teacherRegisterText = (TextView) findViewById(R.id.teacher_register_text);
        teacherRegisterText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.student_login_text:
                startActivity(new Intent(TeacherLoginActivity.this, StudentLoginActivity.class));
                finish();
                break;
            case R.id.teacher_register_text:
                startActivity(new Intent(TeacherLoginActivity.this, TeacherRegisterActivity.class));
                break;
            case R.id.teacher_login_btn:
                teacher_name = teacherName.getText().toString().trim();
                teacher_pass = teacherPass.getText().toString().trim();
                if (TextUtils.isEmpty(teacher_name) || TextUtils.isEmpty(teacher_pass)) {
                    CustomToast.showLongToast(getString(R.string.para_null));
                    return;
                }
                login_teacher(teacher_name, teacher_pass);
                break;
        }
    }

    private void login_teacher(String teacher_name, String teacher_pass) {
        StringRequest request = HttpProcessManager.getInstance().loginTeacher(Constant.HOST_TEACHER_LOGIN, teacher_name, teacher_pass);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

    public void onEventMainThread(TeacherLoginEvent event) {


        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            if(code==Constant.CODE_SUCCESS) {
                startActivity(new Intent(TeacherLoginActivity.this, TeacherMainActivity.class));
                findTeacherInfo();
                finish();
            }
            CustomToast.showToast(event.info);
        }

    }

    private void findTeacherInfo() {

        StringRequest request = HttpProcessManager.getInstance().findTeacherInfo(Constant.HOST_TEACHER_FIND, teacher_name);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

}
